home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-config-printer / cupspk.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  18.1 KB  |  579 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import sys
  6. import tempfile
  7. import cups
  8. import dbus
  9. import gtk
  10. from dbus.mainloop.glib import DBusGMainLoop
  11. DBusGMainLoop(set_as_default = True)
  12. PK_AUTH_NAME = 'org.freedesktop.PolicyKit.AuthenticationAgent'
  13. PK_AUTH_PATH = '/'
  14. PK_AUTH_IFACE = 'org.freedesktop.PolicyKit.AuthenticationAgent'
  15. CUPS_PK_NAME = 'org.opensuse.CupsPkHelper.Mechanism'
  16. CUPS_PK_PATH = '/'
  17. CUPS_PK_IFACE = 'org.opensuse.CupsPkHelper.Mechanism'
  18. CUPS_PK_NEED_AUTH = 'org.opensuse.CupsPkHelper.Mechanism.NotPrivileged'
  19. pk_auth_ret = False
  20. pk_auth_error = None
  21. pk_auth_running = False
  22. pk_auth_done = False
  23.  
  24. def _pk_auth_reply_handler(ret):
  25.     global pk_auth_ret, pk_auth_done
  26.     pk_auth_ret = ret
  27.     pk_auth_done = True
  28.  
  29.  
  30. def _pk_auth_error_handler(e):
  31.     global pk_auth_error, pk_auth_done
  32.     pk_auth_error = str(e)
  33.     pk_auth_done = True
  34.  
  35.  
  36. class Connection:
  37.     
  38.     def __init__(self, host, port, encryption):
  39.         self._parent = None
  40.         
  41.         try:
  42.             self._session_bus = dbus.SessionBus()
  43.             self._system_bus = dbus.SystemBus()
  44.         except dbus.exceptions.DBusException:
  45.             self._session_bus = None
  46.             self._system_bus = None
  47.  
  48.         self._connection = cups.Connection(host = host, port = port, encryption = encryption)
  49.         self._hack_subclass()
  50.  
  51.     
  52.     def _hack_subclass(self):
  53.         methodtype = type(self._connection.getPrinters)
  54.         for fname in dir(self._connection):
  55.             if fname[0] == '_':
  56.                 continue
  57.             
  58.             fn = getattr(self._connection, fname)
  59.             if type(fn) != methodtype:
  60.                 continue
  61.             
  62.             if not hasattr(self, fname):
  63.                 setattr(self, fname, fn.__call__)
  64.                 continue
  65.         
  66.  
  67.     
  68.     def set_parent(self, parent):
  69.         self._parent = parent
  70.  
  71.     
  72.     def _get_cups_pk(self):
  73.         
  74.         try:
  75.             object = self._system_bus.get_object(CUPS_PK_NAME, CUPS_PK_PATH)
  76.             return dbus.Interface(object, CUPS_PK_IFACE)
  77.         except dbus.exceptions.DBusException:
  78.             return None
  79.             except AttributeError:
  80.                 return None
  81.             else:
  82.                 return None
  83.  
  84.  
  85.     
  86.     def _obtain_auth(self, action, xid = 0):
  87.         global pk_auth_ret, pk_auth_error, pk_auth_done, pk_auth_running, pk_auth_running
  88.         if pk_auth_running:
  89.             return False
  90.         pk_auth_ret = False
  91.         pk_auth_error = None
  92.         pk_auth_done = False
  93.         pk_auth_object = self._session_bus.get_object(PK_AUTH_NAME, PK_AUTH_PATH)
  94.         pk_auth = dbus.Interface(pk_auth_object, PK_AUTH_IFACE)
  95.         pk_auth_running = True
  96.         pk_auth.ObtainAuthorization(action, dbus.UInt32(xid), dbus.UInt32(os.getpid()), reply_handler = _pk_auth_reply_handler, error_handler = _pk_auth_error_handler)
  97.         while not pk_auth_done:
  98.             gtk.main_iteration(True)
  99.             continue
  100.             pk_auth_running
  101.         pk_auth_running = False
  102.         if pk_auth_error != None:
  103.             raise dbus.exceptions.DBusException(pk_auth_error)
  104.         pk_auth_error != None
  105.         if not type(pk_auth_ret) == dbus.Boolean:
  106.             return False
  107.         return pk_auth_ret != 0
  108.  
  109.     
  110.     def _handle_exception_with_auth(self, e):
  111.         if e.get_dbus_name() != CUPS_PK_NEED_AUTH:
  112.             return False
  113.         tokens = e.get_dbus_message().split(' ', 2)
  114.         if len(tokens) != 3:
  115.             return False
  116.         
  117.         try:
  118.             xid = 0
  119.             ret = self._obtain_auth(tokens[0], xid)
  120.         except dbus.exceptions.DBusException:
  121.             len(tokens) != 3
  122.             len(tokens) != 3
  123.             e.get_dbus_name() != CUPS_PK_NEED_AUTH
  124.             return False
  125.  
  126.         if not ret:
  127.             raise cups.IPPError(cups.IPP_NOT_AUTHORIZED, 'pkcancel')
  128.         ret
  129.         return True
  130.  
  131.     
  132.     def _call_with_pk_and_fallback(self, use_fallback, pk_function_name, pk_args, fallback_function, *args, **kwds):
  133.         pk_function = None
  134.         if not use_fallback:
  135.             cups_pk = self._get_cups_pk()
  136.             if cups_pk:
  137.                 
  138.                 try:
  139.                     pk_function = cups_pk.get_dbus_method(pk_function_name)
  140.                 except dbus.exceptions.DBusException:
  141.                     pass
  142.                 except:
  143.                     None<EXCEPTION MATCH>dbus.exceptions.DBusException
  144.                 
  145.  
  146.             None<EXCEPTION MATCH>dbus.exceptions.DBusException
  147.         
  148.         if use_fallback or not pk_function:
  149.             return fallback_function(*args, **kwds)
  150.         pk_retval = 'PolicyKit communication issue'
  151.         while True:
  152.             
  153.             try:
  154.                 pk_retval = pk_function(*pk_args)
  155.                 if type(pk_retval) == tuple:
  156.                     retval = pk_retval[1:]
  157.                     if pk_retval[0] == '':
  158.                         if len(retval) == 1:
  159.                             return retval[0]
  160.                         return retval
  161.                     pk_retval[0] == ''
  162.                 
  163.             continue
  164.             except dbus.exceptions.DBusException:
  165.                 not pk_function
  166.                 e = not pk_function
  167.                 if not self._handle_exception_with_auth(e):
  168.                     break
  169.                 
  170.                 self._handle_exception_with_auth(e)
  171.             
  172.  
  173.             not pk_function<EXCEPTION MATCH>dbus.exceptions.DBusException
  174.         if pk_retval != '':
  175.             print >>sys.stderr, 'PolicyKit call to %s did not work: %s' % (pk_function_name, pk_retval)
  176.             return fallback_function(*args, **kwds)
  177.  
  178.     
  179.     def _args_to_tuple(self, types, *args):
  180.         retval = [
  181.             False]
  182.         if len(types) != len(args):
  183.             retval[0] = True
  184.             retval.extend(types)
  185.             return tuple(types)
  186.         exception = False
  187.         for i in range(len(types)):
  188.             if type(args[i]) != types[i]:
  189.                 if types[i] == str and type(args[i]) == unicode:
  190.                     pass
  191.                 elif types[i] == str and type(args[i]) == int:
  192.                     retval.append(str(args[i]))
  193.                     continue
  194.                 elif types[i] == str and type(args[i]) == float:
  195.                     retval.append(str(args[i]))
  196.                     continue
  197.                 elif types[i] == str and type(args[i]) == bool:
  198.                     retval.append(str(args[i]))
  199.                     continue
  200.                 elif types[i] == str and args[i] == None:
  201.                     retval.append('')
  202.                     continue
  203.                 elif types[i] == list and type(args[i]) == tuple:
  204.                     retval.append(list(args[i]))
  205.                     continue
  206.                 elif types[i] == list and args[i] == None:
  207.                     retval.append([])
  208.                     continue
  209.                 else:
  210.                     exception = True
  211.             
  212.             retval.append(args[i])
  213.         
  214.         retval[0] = exception
  215.         return tuple(retval)
  216.  
  217.     
  218.     def _kwds_to_vars(self, names, **kwds):
  219.         ret = []
  220.         for name in names:
  221.             if kwds.has_key(name):
  222.                 ret.append(kwds[name])
  223.                 continue
  224.             ret.append('')
  225.         
  226.         return tuple(ret)
  227.  
  228.     
  229.     def cancelJob(self, *args, **kwds):
  230.         (use_pycups, jobid) = self._args_to_tuple([
  231.             int], *args)
  232.         pk_args = (jobid,)
  233.         self._call_with_pk_and_fallback(use_pycups, 'JobCancel', pk_args, self._connection.cancelJob, *args, **kwds)
  234.  
  235.     
  236.     def setJobHoldUntil(self, *args, **kwds):
  237.         (use_pycups, jobid, job_hold_until) = self._args_to_tuple([
  238.             int,
  239.             str], *args)
  240.         pk_args = (jobid, job_hold_until)
  241.         self._call_with_pk_and_fallback(use_pycups, 'JobSetHoldUntil', pk_args, self._connection.setJobHoldUntil, *args, **kwds)
  242.  
  243.     
  244.     def restartJob(self, *args, **kwds):
  245.         (use_pycups, jobid) = self._args_to_tuple([
  246.             int], *args)
  247.         pk_args = (jobid,)
  248.         self._call_with_pk_and_fallback(use_pycups, 'JobRestart', pk_args, self._connection.restartJob, *args, **kwds)
  249.  
  250.     
  251.     def getFile(self, *args, **kwds):
  252.         """ Keeping this as an alternative for the code.
  253.             We don't use it because it's not possible to know if the call was a
  254.             PK-one (and so we push the content of a temporary filename to fd or
  255.             file) or a non-PK-one (in which case nothing should be done).
  256.  
  257.                 filename = None
  258.                 fd = None
  259.                 file = None
  260.                 if use_pycups:
  261.                     if len(kwds) != 1:
  262.                         use_pycups = True
  263.                     elif kwds.has_key('filename'):
  264.                         filename = kwds['filename']
  265.                     elif kwds.has_key('fd'):
  266.                         fd = kwds['fd']
  267.                     elif kwds.has_key('file'):
  268.                         file = kwds['file']
  269.                     else:
  270.                         use_pycups = True
  271.  
  272.                     if fd or file:
  273.         """
  274.         file_object = None
  275.         fd = None
  276.         if len(args) == 2:
  277.             (use_pycups, resource, filename) = self._args_to_tuple([
  278.                 str,
  279.                 str], *args)
  280.         else:
  281.             (use_pycups, resource) = self._args_to_tuple([
  282.                 str], *args)
  283.             if kwds.has_key('filename'):
  284.                 filename = kwds['filename']
  285.             elif kwds.has_key('fd'):
  286.                 fd = kwds['fd']
  287.             elif kwds.has_key('file'):
  288.                 file_object = kwds['file']
  289.             elif not use_pycups:
  290.                 raise TypeError()
  291.             else:
  292.                 filename = None
  293.         if not use_pycups:
  294.             if fd != None or file_object != None:
  295.                 (tmpfd, tmpfname) = tempfile.mkstemp()
  296.                 os.close(tmpfd)
  297.                 pk_args = (resource, tmpfname)
  298.                 self._call_with_pk_and_fallback(use_pycups, 'FileGet', pk_args, self._connection.getFile, *args, **kwds)
  299.                 tmpfd = os.open(tmpfname, os.O_RDONLY)
  300.                 tmpfile = os.fdopen(tmpfd, 'r')
  301.                 tmpfile.seek(0)
  302.                 if fd != None:
  303.                     os.lseek(fd, 0, os.SEEK_SET)
  304.                     line = tmpfile.readline()
  305.                     while line != '':
  306.                         os.write(fd, line)
  307.                         line = tmpfile.readline()
  308.                 else:
  309.                     file_object.seek(0)
  310.                     line = tmpfile.readline()
  311.                     while line != '':
  312.                         file_object.write(line)
  313.                         line = tmpfile.readline()
  314.                 tmpfile.close()
  315.                 os.remove(tmpfname)
  316.             else:
  317.                 pk_args = (resource, filename)
  318.                 self._call_with_pk_and_fallback(use_pycups, 'FileGet', pk_args, self._connection.getFile, *args, **kwds)
  319.  
  320.     
  321.     def putFile(self, *args, **kwds):
  322.         if len(args) == 2:
  323.             (use_pycups, resource, filename) = self._args_to_tuple([
  324.                 str,
  325.                 str], *args)
  326.         else:
  327.             (use_pycups, resource) = self._args_to_tuple([
  328.                 str], *args)
  329.             if kwds.has_key('filename'):
  330.                 filename = kwds['filename']
  331.             elif kwds.has_key('fd'):
  332.                 fd = kwds['fd']
  333.             elif kwds.has_key('file'):
  334.                 file_object = kwds['file']
  335.             elif not use_pycups:
  336.                 raise TypeError()
  337.             else:
  338.                 filename = None
  339.         if not use_pycups:
  340.             if fd != None or file_object != None:
  341.                 (tmpfd, tmpfname) = tempfile.mkstemp()
  342.                 os.lseek(tmpfd, 0, os.SEEK_SET)
  343.                 if fd != None:
  344.                     os.lseek(fd, 0, os.SEEK_SET)
  345.                     buf = os.read(fd, 512)
  346.                     while buf != '':
  347.                         os.write(tmpfd, buf)
  348.                         buf = os.read(fd, 512)
  349.                 else:
  350.                     file_object.seek(0)
  351.                     line = file_object.readline()
  352.                     while line != '':
  353.                         os.write(tmpfd, line)
  354.                         line = file_object.readline()
  355.                 os.close(tmpfd)
  356.                 pk_args = (resource, tmpfname)
  357.                 self._call_with_pk_and_fallback(use_pycups, 'FilePut', pk_args, self._connection.putFile, *args, **kwds)
  358.                 os.remove(tmpfname)
  359.             else:
  360.                 pk_args = (resource, filename)
  361.                 self._call_with_pk_and_fallback(use_pycups, 'FilePut', pk_args, self._connection.putFile, *args, **kwds)
  362.  
  363.     
  364.     def addPrinter(self, *args, **kwds):
  365.         (use_pycups, name) = self._args_to_tuple([
  366.             str], *args)
  367.         (filename, ppdname, info, location, device, ppd) = self._kwds_to_vars([
  368.             'filename',
  369.             'ppdname',
  370.             'info',
  371.             'location',
  372.             'device',
  373.             'ppd'], **kwds)
  374.         need_unlink = False
  375.         if not ppdname and not filename and ppd:
  376.             (fd, filename) = tempfile.mkstemp()
  377.             ppd.writeFd(fd)
  378.             os.close(fd)
  379.             need_unlink = True
  380.         
  381.         if filename and not ppdname:
  382.             pk_args = (name, device, filename, info, location)
  383.             self._call_with_pk_and_fallback(use_pycups, 'PrinterAddWithPpdFile', pk_args, self._connection.addPrinter, *args, **kwds)
  384.             if need_unlink:
  385.                 os.unlink(filename)
  386.             
  387.         else:
  388.             pk_args = (name, device, ppdname, info, location)
  389.             self._call_with_pk_and_fallback(use_pycups, 'PrinterAdd', pk_args, self._connection.addPrinter, *args, **kwds)
  390.  
  391.     
  392.     def setPrinterDevice(self, *args, **kwds):
  393.         (use_pycups, name, device) = self._args_to_tuple([
  394.             str,
  395.             str], *args)
  396.         pk_args = (name, device)
  397.         self._call_with_pk_and_fallback(use_pycups, 'PrinterSetDevice', pk_args, self._connection.setPrinterDevice, *args, **kwds)
  398.  
  399.     
  400.     def setPrinterInfo(self, *args, **kwds):
  401.         (use_pycups, name, info) = self._args_to_tuple([
  402.             str,
  403.             str], *args)
  404.         pk_args = (name, info)
  405.         self._call_with_pk_and_fallback(use_pycups, 'PrinterSetInfo', pk_args, self._connection.setPrinterInfo, *args, **kwds)
  406.  
  407.     
  408.     def setPrinterLocation(self, *args, **kwds):
  409.         (use_pycups, name, location) = self._args_to_tuple([
  410.             str,
  411.             str], *args)
  412.         pk_args = (name, location)
  413.         self._call_with_pk_and_fallback(use_pycups, 'PrinterSetLocation', pk_args, self._connection.setPrinterLocation, *args, **kwds)
  414.  
  415.     
  416.     def setPrinterShared(self, *args, **kwds):
  417.         (use_pycups, name, shared) = self._args_to_tuple([
  418.             str,
  419.             bool], *args)
  420.         pk_args = (name, shared)
  421.         self._call_with_pk_and_fallback(use_pycups, 'PrinterSetShared', pk_args, self._connection.setPrinterShared, *args, **kwds)
  422.  
  423.     
  424.     def setPrinterJobSheets(self, *args, **kwds):
  425.         (use_pycups, name, start, end) = self._args_to_tuple([
  426.             str,
  427.             str,
  428.             str], *args)
  429.         pk_args = (name, start, end)
  430.         self._call_with_pk_and_fallback(use_pycups, 'PrinterSetJobSheets', pk_args, self._connection.setPrinterJobSheets, *args, **kwds)
  431.  
  432.     
  433.     def setPrinterErrorPolicy(self, *args, **kwds):
  434.         (use_pycups, name, policy) = self._args_to_tuple([
  435.             str,
  436.             str], *args)
  437.         pk_args = (name, policy)
  438.         self._call_with_pk_and_fallback(use_pycups, 'PrinterSetErrorPolicy', pk_args, self._connection.setPrinterErrorPolicy, *args, **kwds)
  439.  
  440.     
  441.     def setPrinterOpPolicy(self, *args, **kwds):
  442.         (use_pycups, name, policy) = self._args_to_tuple([
  443.             str,
  444.             str], *args)
  445.         pk_args = (name, policy)
  446.         self._call_with_pk_and_fallback(use_pycups, 'PrinterSetOpPolicy', pk_args, self._connection.setPrinterOpPolicy, *args, **kwds)
  447.  
  448.     
  449.     def setPrinterUsersAllowed(self, *args, **kwds):
  450.         (use_pycups, name, users) = self._args_to_tuple([
  451.             str,
  452.             list], *args)
  453.         pk_args = (name, users)
  454.         self._call_with_pk_and_fallback(use_pycups, 'PrinterSetUsersAllowed', pk_args, self._connection.setPrinterUsersAllowed, *args, **kwds)
  455.  
  456.     
  457.     def setPrinterUsersDenied(self, *args, **kwds):
  458.         (use_pycups, name, users) = self._args_to_tuple([
  459.             str,
  460.             list], *args)
  461.         pk_args = (name, users)
  462.         self._call_with_pk_and_fallback(use_pycups, 'PrinterSetUsersDenied', pk_args, self._connection.setPrinterUsersDenied, *args, **kwds)
  463.  
  464.     
  465.     def addPrinterOptionDefault(self, *args, **kwds):
  466.         (use_pycups, name, option, value) = self._args_to_tuple([
  467.             str,
  468.             str,
  469.             str], *args)
  470.         if not use_pycups:
  471.             values = (value,)
  472.         else:
  473.             (use_pycups, name, option, values) = self._args_to_tuple([
  474.                 str,
  475.                 str,
  476.                 list], *args)
  477.         pk_args = (name, option, values)
  478.         self._call_with_pk_and_fallback(use_pycups, 'PrinterAddOptionDefault', pk_args, self._connection.addPrinterOptionDefault, *args, **kwds)
  479.  
  480.     
  481.     def deletePrinterOptionDefault(self, *args, **kwds):
  482.         (use_pycups, name, option) = self._args_to_tuple([
  483.             str,
  484.             str], *args)
  485.         pk_args = (name, option)
  486.         self._call_with_pk_and_fallback(use_pycups, 'PrinterDeleteOptionDefault', pk_args, self._connection.deletePrinterOptionDefault, *args, **kwds)
  487.  
  488.     
  489.     def deletePrinter(self, *args, **kwds):
  490.         (use_pycups, name) = self._args_to_tuple([
  491.             str], *args)
  492.         pk_args = (name,)
  493.         self._call_with_pk_and_fallback(use_pycups, 'PrinterDelete', pk_args, self._connection.deletePrinter, *args, **kwds)
  494.  
  495.     
  496.     def addPrinterToClass(self, *args, **kwds):
  497.         (use_pycups, printer, name) = self._args_to_tuple([
  498.             str,
  499.             str], *args)
  500.         pk_args = (name, printer)
  501.         self._call_with_pk_and_fallback(use_pycups, 'ClassAddPrinter', pk_args, self._connection.addPrinterToClass, *args, **kwds)
  502.  
  503.     
  504.     def deletePrinterFromClass(self, *args, **kwds):
  505.         (use_pycups, printer, name) = self._args_to_tuple([
  506.             str,
  507.             str], *args)
  508.         pk_args = (name, printer)
  509.         self._call_with_pk_and_fallback(use_pycups, 'ClassDeletePrinter', pk_args, self._connection.deletePrinterFromClass, *args, **kwds)
  510.  
  511.     
  512.     def deleteClass(self, *args, **kwds):
  513.         (use_pycups, name) = self._args_to_tuple([
  514.             str], *args)
  515.         pk_args = (name,)
  516.         self._call_with_pk_and_fallback(use_pycups, 'ClassDelete', pk_args, self._connection.deleteClass, *args, **kwds)
  517.  
  518.     
  519.     def setDefault(self, *args, **kwds):
  520.         (use_pycups, name) = self._args_to_tuple([
  521.             str], *args)
  522.         pk_args = (name,)
  523.         self._call_with_pk_and_fallback(use_pycups, 'PrinterSetDefault', pk_args, self._connection.setDefault, *args, **kwds)
  524.  
  525.     
  526.     def enablePrinter(self, *args, **kwds):
  527.         (use_pycups, name) = self._args_to_tuple([
  528.             str], *args)
  529.         pk_args = (name, True)
  530.         self._call_with_pk_and_fallback(use_pycups, 'PrinterSetEnabled', pk_args, self._connection.enablePrinter, *args, **kwds)
  531.  
  532.     
  533.     def disablePrinter(self, *args, **kwds):
  534.         (use_pycups, name) = self._args_to_tuple([
  535.             str], *args)
  536.         pk_args = (name, False)
  537.         self._call_with_pk_and_fallback(use_pycups, 'PrinterSetEnabled', pk_args, self._connection.enablePrinter, *args, **kwds)
  538.  
  539.     
  540.     def acceptJobs(self, *args, **kwds):
  541.         (use_pycups, name) = self._args_to_tuple([
  542.             str], *args)
  543.         pk_args = (name, True, '')
  544.         self._call_with_pk_and_fallback(use_pycups, 'PrinterSetAcceptJobs', pk_args, self._connection.acceptJobs, *args, **kwds)
  545.  
  546.     
  547.     def rejectJobs(self, *args, **kwds):
  548.         (use_pycups, name) = self._args_to_tuple([
  549.             str], *args)
  550.         (reason,) = self._kwds_to_vars([
  551.             'reason'], **kwds)
  552.         pk_args = (name, False, reason)
  553.         self._call_with_pk_and_fallback(use_pycups, 'PrinterSetAcceptJobs', pk_args, self._connection.rejectJobs, *args, **kwds)
  554.  
  555.     
  556.     def adminGetServerSettings(self, *args, **kwds):
  557.         use_pycups = False
  558.         pk_args = ()
  559.         result = self._call_with_pk_and_fallback(use_pycups, 'ServerGetSettings', pk_args, self._connection.adminGetServerSettings, *args, **kwds)
  560.         settings = { }
  561.         if result != None:
  562.             for i in result.keys():
  563.                 if type(i) == 'dbus.String':
  564.                     settings[i.encode()] = result[i].encode()
  565.                     continue
  566.                 settings[i] = result[i]
  567.             
  568.         
  569.         return settings
  570.  
  571.     
  572.     def adminSetServerSettings(self, *args, **kwds):
  573.         (use_pycups, settings) = self._args_to_tuple([
  574.             dict], *args)
  575.         pk_args = (settings,)
  576.         self._call_with_pk_and_fallback(use_pycups, 'ServerSetSettings', pk_args, self._connection.adminSetServerSettings, *args, **kwds)
  577.  
  578.  
  579.